﻿using nRoute.Components.Composition;
using nRoute.Components.Routing;
using nRoute.Utilities;

namespace nRoute.Mapping
{
    public abstract class DefaultNavigationLocatorBase
		 : ResourceLocatorBase
    {
        private readonly string _url;
        private IRouteHandler _routeHandler;

        protected DefaultNavigationLocatorBase(string url)
        {
            _url = url;
        }

        public string Url
        {
            get { return _url; }
        }

#region Must Override

        protected abstract IRouteHandler CreateRouteHandler(ITypeBuilder typeBuilder);

#endregion

#region Overrides

        protected override object GetInstance(ITypeBuilder typeBuilder)
        {
            // we are caching the route handler 
            if (_routeHandler == null) _routeHandler = CreateRouteHandler(typeBuilder);
            return _routeHandler;
        }

#endregion

    }
}